package com.cl.service.payGrant;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;


public class GcChatUtils {
	public static HashMap parseAuthorName(String authorName) throws ParseException{
		HashMap hm = new HashMap();
		//2011.3.21 c4w
		if(authorName.indexOf("username")!=-1){
			//j 添加O type
			 int o1=authorName.indexOf('=')+1;
			 int o2=authorName.length();
			 String authorName1 =authorName.substring(o1,o2);
			hm.put("author",authorName1);
			hm.put("type",'O');
			return hm;
		}
		if(authorName==null){
			hm.put("author","");
			hm.put("type",'J');
			return hm;
		}
		Pattern pattern = Pattern.compile("\\[[^\\]]+\\]");//[]
		Matcher matcher = pattern.matcher(authorName);
		boolean hasResult = matcher.find();
		StringBuffer res = new StringBuffer("");
		
		if(hasResult){
			if(authorName.indexOf("cn")>=0)hm.put("type",'G');
			else hm.put("type",'W');
		}else{
			hm.put("author",authorName);
			hm.put("type",'J');
		}
		
		while(hasResult){
			String pt = matcher.group();
			matcher.appendReplacement(res, "");
			addParseAuthorNameResult(hm,parseAuthorName2Map(pt.substring(1,pt.length()-1)));
			hasResult = matcher.find();
			hm.put("author", ObjectUtils.toString(res));
		}
		return hm;
	}
	
	public static String parseStringZkh(String authorName) throws ParseException{
		if(StringUtils.isEmpty(authorName))return "";
		Pattern pattern = Pattern.compile("\\[[^\\]]+\\]");
		Matcher matcher = pattern.matcher(authorName);
		boolean hasResult = matcher.find();
		StringBuffer res = new StringBuffer("");
		while(hasResult){
			String mStr = matcher.group();
			return mStr.substring(1,mStr.length()-1);
		}
		return "";
	}
	
	public static Map parseAuthorName2Map(String pt) throws ParseException{
		Map map = new HashMap();  
		  java.util.StringTokenizer items;  
		  for(StringTokenizer entrys = new StringTokenizer(pt, ",");entrys.hasMoreTokens();   
		    map.put(items.nextToken(), items.hasMoreTokens() ? ((Object) (items.nextToken())) : null))  
		      items = new StringTokenizer(entrys.nextToken(), "=");  
		  return map;  
	}
	
	public static void addParseAuthorNameResult(HashMap hm ,Map pt) throws ParseException{

		hm.put(
				"org", 
				pt.get("org")!=null?
						ObjectUtils.toString(hm.get("org"))+pt.get("org")+";"
						:"".equals(ObjectUtils.toString(hm.get("org")))?"":ObjectUtils.toString(hm.get("org"))+";"
		);
		if(pt.get("wn")!=null){
			if(StringUtils.equals((String) pt.get("wn"), "")){
				hm.put("wn",0);
			}else{
				DecimalFormat   myformat=new   java.text.DecimalFormat("#0"); 
				//hm.put("wn",myformat.format(jo.getDouble("wn")));	//2011.3.21 [wn=11010219701201043x] error
				hm.put("wn",pt.get("wn"));
			}
		}
		// j 0628
		if(pt.get("cn")!=null){
			hm.put(
					"cn", 
					pt.get("cn")!=null?pt.get("cn"):""
					);
		}
//		hm.put(
//				"cn", 
//				pt.get("cn")!=null?pt.get("cn"):""
//		);
	}

}
